// 2025/3/26
// https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/
// 买卖股票的最佳时机2

class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int buy = 0, sale = 0;
            int ans = 0, n = prices.size();
            for(; sale < n - 1; sale++)
            {
                if(prices[sale] > prices[sale + 1])
                {
                    ans += prices[sale] - prices[buy];
                    buy = sale + 1;
                }
            }
            ans += prices[sale] - prices[buy];
            return ans;
        }
    };